package cn.itcast.nems.studentrecord.web.controller.b;

import cn.hutool.core.collection.CollectionUtil;
import cn.itcast.ic.common.bean.Page;
import cn.itcast.nems.base.dto.E2ClazzDto;
import cn.itcast.nems.common.PageQueryModel;
import cn.itcast.nems.common.web.vo.BasePageRequest;
import cn.itcast.nems.manager.util.CustomBeanUtil;
import cn.itcast.nems.studentrecord.dto.ClazzEnrollmentCountDTO;
import cn.itcast.nems.studentrecord.dto.StudentRecordDTO;
import cn.itcast.nems.studentrecord.dto.StudentRecordQueryDTO;
import cn.itcast.nems.studentrecord.service.ClazzEnrollmentCountService;
import cn.itcast.nems.studentrecord.service.studentrecord.StudentRecordDiscountService;
import cn.itcast.nems.studentrecord.web.util.StudentRecordWebUtil;
import cn.itcast.nems.studentrecord.web.vo.query.ClazzEnrollmentCountPageQueryVO;
import cn.itcast.nems.studentrecord.web.vo.query.StudentRecordPageQueryVO;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 缴费情况 controller
 * </p>
 *
 * @author zhouwei
 * @since 2023/11/2 3:24 PM
 **/
@RestController
@RequestMapping("/b/enrollment")
@RequiredArgsConstructor
public class ClazzEnrollmentCountController {

    private final ClazzEnrollmentCountService enrollmentCountService ;
    private final StudentRecordDiscountService studentRecordDiscountService;

    /**
     * 缴费情况 分页查询
     * @param queryParam 检搜条件
     * @return 分页结果
     */
    @PostMapping("/page/list")
    public ResponseEntity<Page<ClazzEnrollmentCountDTO>> findPageList(@RequestBody BasePageRequest<ClazzEnrollmentCountPageQueryVO> queryParam) {
        queryParam.convert();
        ClazzEnrollmentCountDTO countDTO = Objects.isNull(queryParam.getCondition())? new ClazzEnrollmentCountDTO():
                CustomBeanUtil.copyProperties(queryParam.getCondition(), new ClazzEnrollmentCountDTO());
        PageQueryModel<ClazzEnrollmentCountDTO> queryModel = new PageQueryModel<>(queryParam.getPageNumber(),
                queryParam.getPageSize(), countDTO);
        Page<ClazzEnrollmentCountDTO> pageList = enrollmentCountService.findCountPageList(queryModel);
        return ResponseEntity.ok().body(pageList);
    }

    /**
     * <p>
     * 查询分页条件班级名称
     * </p>
     *
     * @param text 检索文本
     * @author zhouwei
     * @since 2024/3/4 10:07 AM
     **/
    @RequestMapping("/clazz/{text}")
    public ResponseEntity<List<E2ClazzDto>> findClazzList4PageSearch(@PathVariable String text) {
        ClazzEnrollmentCountDTO countDTO = new ClazzEnrollmentCountDTO();
        countDTO.setClazzName(text);
        PageQueryModel<ClazzEnrollmentCountDTO> queryModel = new PageQueryModel<>();
        queryModel.convert(50);
        queryModel.setCondition(countDTO);
        Page<ClazzEnrollmentCountDTO> pageList = enrollmentCountService.findCountPageList(queryModel);
        List<E2ClazzDto> list = new ArrayList<>();
        if (Objects.nonNull(pageList) && CollectionUtil.isNotEmpty(pageList.getRecords())) {
            list = pageList.getRecords().stream().map(item -> new E2ClazzDto(item.getClazzId(), item.getClazzName())).sorted().toList();
        }
        return ResponseEntity.ok(list);
    }

    /**
     * <p>
     * 手动同步缴费情况
     * </p>
     *
     * @author zhouwei
     * @since 2024/1/3 6:18 PM
     **/
    @PutMapping("/refresh/{execute_flag}")
    public ResponseEntity<String> refresh(@PathVariable("execute_flag") Boolean executeFlag) {
        return  ResponseEntity.ok().body(enrollmentCountService.manualRefresh(executeFlag) ) ;
    }

    /**
     * <p>
     * 获取手动同步时间
     * </p>
     *
     * @author zhouwei
     * @since 2024/1/3 6:18 PM
     **/
    @GetMapping("/refresh_time")
    public ResponseEntity<String> refreshTime() {
        return  ResponseEntity.ok().body(enrollmentCountService.manualRefresh(false) ) ;
    }

    /**
     * 查看缴费情况详情-学员缴费记录
     *
     * @param id 缴费情况 ID
     * @return 学员缴费列表
     */
    @PostMapping("/{id}/page/list")
    public ResponseEntity<Page<StudentRecordDTO>> detail(@PathVariable String id,
                                                         @RequestBody BasePageRequest<StudentRecordPageQueryVO> queryParam) {
        queryParam.convert();
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<StudentRecordDTO> page
                = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>();
        page.setCurrent(queryParam.getPageNumber());
        page.setSize(queryParam.getPageSize());
        StudentRecordQueryDTO queryDTO  = StudentRecordWebUtil.convert(queryParam.getCondition() == null ? new StudentRecordPageQueryVO() : queryParam.getCondition()) ;
        queryDTO.setStatisticsFlag(true);
        queryDTO.setIncludeInvalid(true);
        queryDTO.setIncludeWaitDeleted(true);
        PageQueryModel<StudentRecordQueryDTO> queryModel = new PageQueryModel<>(
                queryParam.getPageNumber(),
                queryParam.getPageSize(),
                queryDTO
        );
        Page<StudentRecordDTO> pageList = enrollmentCountService.findPageListByClazzEnrollmentCountId(id, queryModel);
        return ResponseEntity.ok().body(pageList);
    }

    /**
     * 学籍绑定的优惠
     * @param recordId 学籍ID
     * @return 优惠列表
     **/
    @GetMapping("/{record_id}/discounts")
    public ResponseEntity<?> discountsList(@PathVariable("record_id") String recordId) {
        return ResponseEntity.ok().body(studentRecordDiscountService.findAllDiscountByRecordId(recordId) );
    }

}
